package com.johnny.xia.util.pay;

import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName UmsPay
 * @Author johnny xia
 * @mailto johnny.xia@qq.com
 * @Date 2021/9/29 11:28 上午
 * @Description TODO
 **/
public class UmsPay {

  public static Logger logger = LoggerFactory.getLogger(UmsPay.class);
  //组织请求报文

  /**
   * msgTypeGetQRCode: bills.getQRCode msgTypeQuery: bills.query msgTypeRefund: bills.refund
   * #消息类型:退款 msgTypeRefundQuery: bills.query #消息类型:退款查询 msgSrcId: 3194 billNotifyUrl:
   * https://grouppurchase2-uat-api.baozun.com/nikeGroup/nikeGroup/pay/pc/notifyUrl/{storeCode}.do
   * signType: SHA256 #签名算法
   */

  public static void main(String[] args) {
    String signType = "SHA256";
    String key = "sYza2bfPfP2iecrGdySkSpK4Fk4j4CrNJbBK8aprQAbzkS5a";
    Map<String, String> paramsMap = new HashMap<>();

//    paramsMap.put("mid", "89831995311APUQ");
    paramsMap.put("mid", "89831995311APUR");
//    paramsMap.put("tid", "APUQ0001");
    paramsMap.put("tid", "APUR0001");
    paramsMap.put("msgType", "bills.getQRCode");
    paramsMap.put("msgSrc", "WWW.LFMYSH.COM");
    paramsMap.put("notifyUrl", "http://localhost:8091/test");
    paramsMap.put("billNo", "12N8" +  System.currentTimeMillis());
    paramsMap.put("billDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
    paramsMap.put("totalAmount", "1.00");

    paramsMap.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    paramsMap.put("signType", signType);
    paramsMap.put("sign", PaymentUtil.makeSign(key, paramsMap));

    String strReqJsonStr = JSON.toJSONString(paramsMap);
    System.out.println("getQrCode strReqJsonStr：" + strReqJsonStr);

    //调用银商平台获取二维码接口
    HttpURLConnection httpURLConnection = null;
    BufferedReader in = null;
    PrintWriter out = null;

    try {
      logger.info("银联支付pc接口调用开始");
      URL url = new URL("https://qr.chinaums.com/netpay-route-server/api/");
      httpURLConnection = (HttpURLConnection) url.openConnection();
      httpURLConnection.setRequestMethod("POST");
      httpURLConnection.setDoInput(true);
      httpURLConnection.setDoOutput(true);
      httpURLConnection.setRequestProperty("Content_Type", "application/json");
      httpURLConnection.setRequestProperty("Accept_Charset", "UTF-8");
      httpURLConnection.setRequestProperty("contentType", "UTF-8");
      httpURLConnection.setConnectTimeout(3000);
      httpURLConnection.setReadTimeout(3000);
      //发送POST请求参数
      out = new PrintWriter(httpURLConnection.getOutputStream());
      out.write(strReqJsonStr);
      out.flush();

      //读取响应
      if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        StringBuffer content = new StringBuffer();
        String tempStr = null;
        in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        while ((tempStr = in.readLine()) != null) {
          content.append(tempStr);
        }
        logger.info(" getQrCode content{}", content.toString());

        //转换成json对象
        com.alibaba.fastjson.JSONObject respJson = JSON.parseObject(content.toString());
        String resultCode = respJson.getString("errCode");
        System.out.println(respJson.toJSONString());
      }
      logger.info("银联支付pc接口调用结束");
    } catch (Exception e) {
      logger.error("msg", "调用银商接口出现异常{}：", e);

    }
  }
}
